package com.esm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.ConnectionEvent;

import com.esm.dao.SelectUserDao_zxk;
import com.esm.model.User;
import com.esm.utils.JDBCUtil;
/**
 * 查询uid根据uname  Dao实现
 * @author 康少
 *
 * 2017年1月7日
 */
public class SelectUserDaoimpl_zxk implements SelectUserDao_zxk{

	public int getUidByUname(String uname) {
		// TODO Auto-generated method stub
		int uid = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ems_user.uid");
		sql.append(" FROM ems_user");
		sql.append(" WHERE ems_user.uname = ?");
		Connection con = JDBCUtil.getConnection();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				uid = rs.getInt("uid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return uid;
	}

	public List<User> getUname() {
		// TODO Auto-generated method stub
		List<User> uname = new ArrayList<User>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ems_user.uname");
		sql.append(" FROM ems_user");
		Connection con = JDBCUtil.getConnection();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				User u = new User();
				u.setUname(rs.getString("uname"));
				uname.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return uname;
	}

}
